package com.gujiangbo.disruptor;

import com.lmax.disruptor.RingBuffer;

/**
 * 生产者
 */
public class StringEventProducer {

    /**
     * 环形数组
     */
    public final RingBuffer<StringEvent> ringBuffer;


    public StringEventProducer(RingBuffer<StringEvent> ringBuffer) {
        this.ringBuffer = ringBuffer;
    }

    /**
     * 生产数据的方法--> 向环形数组放入数据
     *
     * @param data
     */
    public void onData(String data) {
        long sequeue = ringBuffer.next();
        System.out.println("当前放入数据的位置:" + sequeue);
        try {
            StringEvent event = ringBuffer.get(sequeue);
            event.setValue(data);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            //发布事件
            ringBuffer.publish(sequeue);
        }
    }
}
